package example;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import redstone.xmlrpc.XmlRpcArray;

import com.google.gdata.data.PlainTextConstruct;

import net.bican.wordpress.Category;
import net.bican.wordpress.Page;
import net.bican.wordpress.Wordpress;

import models.Blog;

public class WordpressEx extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws ServletException, IOException {
		Blog blog = new Blog();
		blog.setBlogId("http://localhost/wordpress_auto");
		blog.setUsername("admin");
		blog.setPassword("abcD1234");
		try {
			Wordpress wp = new Wordpress(blog.getUsername(), blog.getPassword(), blog.getBlogId() + "/xmlrpc.php");
			
			/*Page recentPost = new Page();
			recentPost.setDateCreated(new Date());
			String status = "draft";
		    recentPost.setPost_status(status);
		    recentPost.setWp_author("admin");
		    recentPost.setTitle(new PlainTextConstruct("Hello the world").getText());
		    recentPost.setDescription(new PlainTextConstruct("Description hello the world").getText());
		    recentPost.setMt_keywords("hoanbn, hoantocxoan, 123");
		    
		    XmlRpcArray categories = new XmlRpcArray();
		    categories.add("hoanbn");
		    categories.add("hoantocxoan");
		    categories.add("buingochoan");
		    recentPost.setCategories(categories);
		    
		    String result = wp.newPost(recentPost, true);
		    response.getWriter().println(result);*/
			
			//wp.newCategory("hoanbui", "", 0);
			
		    List<Category> categories = wp.getCategories();
		    for (int i = 0; i < categories.size(); i++) {
		    	response.getWriter().println(categories.get(i).getCategoryName());
		    }
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
